package com.yj.web.dao.mapper;

import com.yj.web.dao.po.admin.Article;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by yanlinyang on 2017/7/24.
 */
@Component
public interface ArticleMapper {
    @Select("<script>" +
            "select * from cus_article " +
            "<trim prefix=\"where\" prefixOverrides=\"and | or\">" +
            "<if test = \"title != ''\">" +
            "instr(title, #{title}) " +
            "</if>" +
            "<if test = \"status != null\">" +
            "and status = #{status} " +
            "</if>" +
            "<if test = \"offset != '-1'\">" +
            "and deleted_flag = 'N' " +
            "</if>" +
            "</trim>" +
            "order by position asc, created_at desc limit #{size} offset #{offset}" +
            "</script>")
    public List<Article> listArticle(@Param("title") String title, @Param("status") Integer status, @Param("offset") int offset, @Param("size") int size);

    @Select("<script>" +
            "select count(*) from cus_article " +
            "<trim prefix=\"where\" prefixOverrides=\"and | or\">" +
            "<if test = \"title != ''\">" +
            "instr(title, #{title}) " +
            "</if>" +
            "<if test = \"status != null\">" +
            "and status = #{status} " +
            "</if>" +
            "<if test = \"status != '-1'\">" +
            "and deleted_flag = 'N' " +
            "</if>" +
            "</trim>" +
            "</script>")
    public int countArticle(@Param("title") String title, @Param("status") Integer status);

    @Delete("update cus_article set deleted_flag = 'Y' where id = #{id} and deleted_flag = 'N'")
    public int deleteArticleById(@Param("id") int id);

    @Insert("insert into cus_article (create_id, created_at, expired_at, content, title, save_url, link_url, position, status) " +
            "values (#{article.create_id}, now(),#{article.expired_at}, #{article.content}, #{article.title}, #{article.save_url}, #{article.link_url}, #{article.position}," +
            "#{article.status})")
    @Options(useGeneratedKeys = true, keyProperty = "article.id")
    public long insertArticle(@Param("article") Article article);

    @Update("update cus_article set update_id=#{article.update_id}, updated_at=now(), expired_at=#{article.expired_at}, content=#{article.content}, title=#{article.title}, " +
            " save_url=#{article.save_url}, link_url=#{article.link_url}, position=#{article.position}, status=#{article.status} where id = #{article.id} and deleted_flag = 'N'")
    public boolean updateArticle(@Param("article") Article article);

    @Select("select * from cus_article where id = #{id} and deleted_flag = 'N'")
    public Article findArticleById(@Param("id") int id);

    @Update("update cus_article set status=#{status} where id = #{id}")
    public boolean updateArticleStatus(@Param("id") int  id, @Param("status") int  status);
}
